package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.control.HeaderView;
import com.jshx.sz.gdjt.db.DataBaseUtil;
import com.jshx.sz.gdjt.thread.CommitDataThread;
import com.jshx.sz.gdjt.util.file.UploadMultiFileThread;

/**
 * 多个tab切换的任务数据采集主页面
 * @author xiangchangan
 * @time 2013-08-21
 */
public class TaskActivityGroup extends FragmentActivity implements OnClickListener
{
	private final static String TAG = "TaskActivityGroup";

	//底部组件切换卡1
	private Button tab1;

	//底部组件切换卡2
	private Button tab2;

	//底部组件切换卡3
	private Button tab3;

	//底部组件切换卡4
	private Button tab4;

	//底部组件切换卡5
	private Button tab5;
	
	//基础Frame
	private BaseFrame frame1;
	
	//定值Frame
	private ConstantFrame frame2;
	
	//传动Frame
	private DriveFrame frame3;
	
	//备注Frame
	private RemarkFrame frame4;
	
	//结论Frame
	private ResaultFrame frame5;

	//标题栏组件
	private HeaderView head;
	
	private UploadMultiFileThread uploadMultiThread ;
	
	//基础fragment数据
	private HashMap<String, String> dataFragment1;
	
	//定值fragment数据
	private HashMap<String, String> dataFragment2;
	
	//传动fragment数据
	private HashMap<String, String> dataFragment3;
	
	//备注fragment数据
	private HashMap<String, String> dataFragment4;
	
	//结论fragment数据
	private HashMap<String, String> dataFragment5;
	
	//团队Id
	private String teamId;
	
	//详情数据
	private HashMap<String, String> data;
	
	//页面类型
	private String type;
	
	//订单编号
	private String orderNum;
	
	//设备编号
	private String assetCode;
	
	private Handler handler = new Handler() {        
		@Override
        public void handleMessage(Message msg) {
			
//        	Bundle data=msg.getData();
//        	String result;
//        	switch (msg.what) { 
// 			case UploadMultiFileThread.UPLOADSUCC://图片上传成功返回
// 			{
//				UploadItem item=data.getParcelable(UploadMultiFileThread.MSG_ITEM);
//				ProgressDialogUtil.show(TaskActivityGroup.this, "第"+item.getTag1()+"张图片上传成功", false);
//				if(item.getTag2()!=null)
//				{
//						ProgressDialogUtil.dismiss();
//						ToastUtil.showLongToast(TaskActivityGroup.this, "上传成功！");
//						TaskActivityGroup.this.finish();
//					 
//				}
//				break;
// 			}
//			case UploadMultiFileThread.UPLOADFAIL://图片上传失败
//			{
//				UploadItem item=data.getParcelable(UploadMultiFileThread.MSG_ITEM);
//				ProgressDialogUtil.show(TaskActivityGroup.this, "第"+item.getTag1()+"张图片上传失败", false);
//				if(item.getTag2()!=null)
//				{
//					ProgressDialogUtil.dismiss();
//					ToastUtil.showLongToast(TaskActivityGroup.this, "有图片上传失败，请再次点击提交！");
//				}
//				break;
//			}
//		}
	}};
	
	public String getTeamId() {
		return teamId;
	}

	public void setTeamId(String teamId) {
		this.teamId = teamId;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	@Override
	protected void onCreate(Bundle arg0) 
	{
		super.onCreate(arg0);
		setContentView(R.layout.group_activity);
		orderNum = getIntent().getStringExtra("orderNo");
		assetCode = getIntent().getStringExtra("assetCode");
		type = getIntent().getStringExtra("type");
		if(type != null && !type.equals("check"))
		{
			data = DataBaseUtil.queryType2TaskDetail(orderNum);
		}
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		data.put(DataBaseUtil.task_detail_type2_table_clos[0], orderNum);
		teamId = getIntent().getStringExtra("teamId");
		findView();
		initView();
		uploadMultiThread=new UploadMultiFileThread(handler);
		uploadMultiThread.start();
	}

	/**
	 * 组件实例化
	 */
	private void findView() {
		tab1 = (Button)findViewById(R.id.group_activity_tab1);
		tab2 = (Button)findViewById(R.id.group_activity_tab2);
		tab3 = (Button)findViewById(R.id.group_activity_tab3);
		tab4 = (Button)findViewById(R.id.group_activity_tab4);
		tab5 = (Button)findViewById(R.id.group_activity_tab5);
		head = (HeaderView)findViewById(R.id.group_activity_header);
	}
	
	public HashMap<String, String> getDataFragment1() {
		return dataFragment1;
	}

	public HashMap<String, String> getDataFragment2() {
		return dataFragment2;
	}

	public HashMap<String, String> getDataFragment3() {
		return dataFragment3;
	}

	public HashMap<String, String> getDataFragment4() {
		return dataFragment4;
	}

	public HashMap<String, String> getDataFragment5() {
		return dataFragment5;
	}

	/**
	 * 组件数据初始化
	 */
	private void initView() {
		
		if(type != null && (type.equals("check")|| type.equals("checkHis")))
		{
			head.setTitle("采集-35KV校验报告");
		}
		else
		{
			head.setTitle("审核-35KV校验报告");
		}
		head.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				TaskActivityGroup.this.finish();
			}
		}, R.drawable.backbtn_select, "返回");
		head.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				 try {
					proClick( v);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				//调用--更新本地手机端 任务列表
				DataBaseUtil dao =new DataBaseUtil();
				dao.updateTaskList(orderNum, type,"4");
				
				 //此处还有一步要提交到服务器
				//调用--数据提交到服务器
				CommitDataThread thread=new CommitDataThread( data, type, "4",assetCode);
				thread.start();
				//图片上传
				thread.imgToServer(uploadMultiThread,getImgList());
				
				TaskActivityGroup.this.finish(); 
			}
		}, R.drawable.titlebtn_select, "提交");
		tab1.setOnClickListener(this);
		tab2.setOnClickListener(this);
		tab3.setOnClickListener(this);
		tab4.setOnClickListener(this);
		tab5.setOnClickListener(this);
		slipDataToFragmentData();
		updateFrame(1);
	}
	
	/**
	 * 获得图片列表
	 * @return
	 */
	private List<String> getImgList()
	{
		List<String> imgNameList=new ArrayList<String>();
		for(int i=1;i<6;i++)
		{
			if(data.containsKey("cdimg"+i))
			{
				String cdimg=data.get("cdimg"+i);
				if(cdimg!=null&&!cdimg.equals(""))
				{
					String[] obj=cdimg.split(",");
					for(String temp:obj)
					{
						if(temp!=null&&!temp.equals(""))
						{
							imgNameList.add(temp);
						}
					}
				}
			}
		}
		return imgNameList;
	}

	
	/**
	 * 把数据切割成各个fragment的数据
	 */
	private void slipDataToFragmentData()
	{
		if(data != null)
        {
    		dataFragment1 = new HashMap<String, String>();
    		for (int i = 2; i < 10; i++) {
    			dataFragment1.put(DataBaseUtil.task_detail_type2_table_clos[i], data.get(DataBaseUtil.task_detail_type2_table_clos[i]));
    		}
    		dataFragment2 = new HashMap<String, String>();
    		for (int i = 10; i < 26; i++) {
    			dataFragment2.put(DataBaseUtil.task_detail_type2_table_clos[i], data.get(DataBaseUtil.task_detail_type2_table_clos[i]));
    		}
    		dataFragment3 = new HashMap<String, String>();
    		for (int i = 26; i < 36; i++) {
    			dataFragment3.put(DataBaseUtil.task_detail_type2_table_clos[i], data.get(DataBaseUtil.task_detail_type2_table_clos[i]));
    		}
    		dataFragment4 = new HashMap<String, String>();
    		for (int i = 36; i < 37; i++) {
    			dataFragment4.put(DataBaseUtil.task_detail_type2_table_clos[i], data.get(DataBaseUtil.task_detail_type2_table_clos[i]));
    		}
    		dataFragment5 = new HashMap<String, String>();
    		for (int i = 37; i < 40; i++) {
    			dataFragment5.put(DataBaseUtil.task_detail_type2_table_clos[i], data.get(DataBaseUtil.task_detail_type2_table_clos[i]));
    		}
        }
	}
	
	/**
	 * 数据保存
	 * @param v
	 * @throws Exception
	 */
	private void proClick(View v)
	{
		if(frame1 != null)
		{
			dataFragment1 = frame1.getData();
		}
		if(frame2 != null)
		{
			dataFragment2 = frame2.getData();
		}
		if(frame3 != null)
		{
			dataFragment3 = frame3.getData();
		}
		if(frame4 != null)
		{
			dataFragment4 = frame4.getData();
		}
		if(frame5 != null)
		{
			dataFragment5 = frame5.getData();
		}
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		if(dataFragment1 != null)
		{
			for(String key : dataFragment1.keySet())
			{
//				System.out.println(key + " = " + dataFragment1.get(key));
				data.put(key, dataFragment1.get(key));
			}
		}
		if(dataFragment2 != null)
		{
			for(String key : dataFragment2.keySet())
			{
//				System.out.println(key + " = " + dataFragment2.get(key));
				data.put(key, dataFragment2.get(key));
			}
		}
		if(dataFragment3 != null)
		{
			for(String key : dataFragment3.keySet())
			{
//				System.out.println(key + " = " + dataFragment3.get(key));
				data.put(key, dataFragment3.get(key));
			}
		}
		if(dataFragment4 != null)
		{
			for(String key : dataFragment4.keySet())
			{
//				System.out.println(key + " = " + dataFragment4.get(key));
				data.put(key, dataFragment4.get(key));
			}
		}
		if(dataFragment5 != null)
		{
			for(String key : dataFragment5.keySet())
			{
//				System.out.println(key + " = " + dataFragment5.get(key));
				data.put(key, dataFragment5.get(key));
			}
		}
		DataBaseUtil.updateType2TaskDetail(data);
	}
	
	
	 
	
	@Override
	public void onClick(View v) 
	{
		int viewId = v.getId();
		switch (viewId) 
		{
		case R.id.group_activity_tab1:
			tab1.setBackgroundResource(R.drawable.tab_press);
			tab2.setBackgroundResource(R.drawable.tab_normal);
			tab3.setBackgroundResource(R.drawable.tab_normal);
			tab4.setBackgroundResource(R.drawable.tab_normal);
			tab5.setBackgroundResource(R.drawable.tab_normal);
			proClick(null);
			updateFrame(1);
			break;
		case R.id.group_activity_tab2:
			tab1.setBackgroundResource(R.drawable.tab_normal);
			tab2.setBackgroundResource(R.drawable.tab_press);
			tab3.setBackgroundResource(R.drawable.tab_normal);
			tab4.setBackgroundResource(R.drawable.tab_normal);
			tab5.setBackgroundResource(R.drawable.tab_normal);		
			proClick(null);
			updateFrame(2);
			break;
		case R.id.group_activity_tab3:
			tab1.setBackgroundResource(R.drawable.tab_normal);
			tab2.setBackgroundResource(R.drawable.tab_normal);
			tab3.setBackgroundResource(R.drawable.tab_press);
			tab4.setBackgroundResource(R.drawable.tab_normal);
			tab5.setBackgroundResource(R.drawable.tab_normal);
			proClick(null);
			updateFrame(3);
			break;
		case R.id.group_activity_tab4:
			tab1.setBackgroundResource(R.drawable.tab_normal);
			tab2.setBackgroundResource(R.drawable.tab_normal);
			tab3.setBackgroundResource(R.drawable.tab_normal);
			tab4.setBackgroundResource(R.drawable.tab_press);
			tab5.setBackgroundResource(R.drawable.tab_normal);
			proClick(null);
			updateFrame(4);
			break;
		case R.id.group_activity_tab5:
			tab1.setBackgroundResource(R.drawable.tab_normal);
			tab2.setBackgroundResource(R.drawable.tab_normal);
			tab3.setBackgroundResource(R.drawable.tab_normal);
			tab4.setBackgroundResource(R.drawable.tab_normal);
			tab5.setBackgroundResource(R.drawable.tab_press);
			proClick(null);
			updateFrame(5);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 根据类型更新不同的Frame
	 * @param type
	 */
	private void updateFrame(int type)
	{
		Log.d(TAG, "fragment type = " + type);
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction  ft = fm.beginTransaction();
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		if(type == 1)
		{
			if(frame1 == null)
			{
				frame1 = new BaseFrame();
			}
			ft.replace(R.id.group_activity_content, frame1);
		}
		else if(type == 2)
		{
			if(frame2 == null)
			{
				frame2 = new ConstantFrame();
			}
			ft.replace(R.id.group_activity_content, frame2);
		}
		else if(type == 3)
		{
			if(frame3 == null)
			{
				frame3 = new DriveFrame();
			}
			ft.replace(R.id.group_activity_content, frame3);
		}
		else if(type == 4)
		{
			if(frame4 == null)
			{
				frame4 = new RemarkFrame();
			}
			ft.replace(R.id.group_activity_content, frame4);
		}
		else if(type == 5)
		{
			if(frame5 == null)
			{
				frame5 = new ResaultFrame();
			}
			ft.replace(R.id.group_activity_content, frame5);
		}
        ft.commit();
	}


	
}
